package delayMessage.queue;

import java.util.concurrent.DelayQueue;

/**
 * 延迟任务
 *
 * 简单的延迟消息的实现有三部分：实现了Delayed接口的消息体、存放消息的延迟队列、消费消息的消费者
 *
 * Created by Zhou_Bing on 2018/9/29.
 */
public class DelayQueueTest {

    public static void main(String[] args) throws NoSuchMethodException {

        DelayQueue<Message> delayQueue = new DelayQueue<>();
        delayQueue.add(new Message(1,"hello",3000));
        delayQueue.add(new Message(2,"world",6000));

        new Thread(new MessageConsumer(delayQueue)).start();


        DelayQueue<Job> jobs = new DelayQueue<>();
        /*jobs.add(new Job(1,JobTest.class,
                JobTest.class.getMethod("sayHello"),
                new Object[]{},2000));

        jobs.add(new Job(2,JobTest.class,
                JobTest.class.getMethod("sayMessage", String.class),
                new Object[]{"world"},4000));*/
        jobs.add(new Job(1, new JobCallBack() {
            @Override
            public void execute() {
                System.out.println("hello");

            }
        },2000));

        String msg = "world";
        jobs.add(new Job(2, new JobCallBack() {
            @Override
            public void execute() {
                System.out.println(msg);
            }
        },4000));

        new Thread(new JobConsumer(jobs)).start();
    }

}
